package com.baosight.sczx.zm.cg.service;

import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.WorkSchedule;
import com.baosight.sczx.zm.cg.domain.ZMCGAccident;

/**
 * 生产事故记录
 * <p>Title: ServiceZMCG71.java</p> 
 */
public class ServiceZMCG72 extends ServiceEPBase {

	/**
	 * 初始化方法 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}

	/**
	 * 查询方法 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
//		String date = inInfo.getString("fdVirtualDate");
//		if (date == null || date.trim().equals("")) {
//			//传入当前日期
//			date = StaticTools.getDate();
//			//设置进EiInfo
//			inInfo.set("fdVirtualDate", date);
//		}
		String date = inInfo.getString("querydate");
		if(date != null && !date.trim().equals("")) {
			StaticTools.setQueryParameters(inInfo, "fdVirtualDate", date, true);
		}
		StaticTools.setQueryParameters(inInfo, "fdMiningId", fdMiningId, true);//添加查询条件
//		StaticTools.setQueryParameters(inInfo, "fdVirtualDate", date, false);
		EiInfo outInfo = super.query(inInfo, "ZMCGAccident.query", new ZMCGAccident());
		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cg.workNum", fdMiningId, true));
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cg.status", new String[]{"草拟", "生效"}, new String[]{"草拟", "生效"}, true));//状态
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cg.department", "orgCode", "orgName", true, UserSession.getInSessionProperty("miningCode").toString()));
		return outInfo;
	}


	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午03:07:57 张坤
	 * </p>
	 *
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo out = new EiInfo(); 		
		ZMCGAccident _dom = new ZMCGAccident();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CG_ACCIDENT"));
//			_dom.setFdCurrentDate(StaticTools.getDate());
//			_dom.setFdReportPerson(UserSession.getInSessionProperty("UserCname").toString());
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(), _dom.getFdWorknumId()));
			_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());
			_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());
			
			this.dao.insert("ZMCGAccident.insert",_dom); 
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}

	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:20:50 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		ZMCGAccident _dom = new ZMCGAccident();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
	
//			_dom.setFdCurrentDate(StaticTools.getDate());
//			_dom.setFdReportPerson(UserSession.getInSessionProperty("UserCname").toString());
			_dom.setFdVirtualDate(WorkSchedule.getVirtualDate(_dom.getFdCurrentDate(),_dom.getFdWorknumId()));
			
			this.dao.update("ZMCGAccident.update",_dom);		
		}
		EiInfo out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
		
	}	

	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:30:17 张坤
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		
		EiInfo _temp = super.delete(inInfo, "ZMCGAccident.delete");
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}	

	/**
	 * <p>Function: report</p>
	 * <p>Description: 生效</p>
	 * <p>return value: ServiceZMCP02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-03-21下午04:30:17 张坤
	 * </p>
	 *
	 */
	public EiInfo report(EiInfo inInfo)
	{	
		EiInfo out=new EiInfo(); 
		ZMCGAccident _dom = new ZMCGAccident();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
	
			this.dao.update("ZMCGAccident.report", _dom);
		}	
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}
}

